package eu.easyedu.dictionary.api.importer.impl;

import eu.easyedu.dictionary.api.importer.DictionaryImporter;
import eu.easyedu.dictionary.api.importer.ImporterCallback;
import eu.easyedu.dictionary.api.model.Language;
import eu.easyedu.dictionary.api.model.Relationship;
import eu.easyedu.dictionary.api.model.Word;
import eu.easyedu.dictionary.api.model.WordRelationship;
import eu.hlavki.xdxf.parser.DefaultXDXFParser;
import eu.hlavki.xdxf.parser.ParseException;
import eu.hlavki.xdxf.parser.XDXFParser;
import eu.hlavki.xdxf.parser.event.XDXFArticleEvent;
import eu.hlavki.xdxf.parser.event.XDXFDictionaryEvent;
import eu.hlavki.xdxf.parser.event.XDXFEventListener;
import eu.hlavki.xdxf.parser.model.XDXFArticle;
import eu.hlavki.xdxf.parser.model.XDXFDictionary;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;
import org.openide.util.Exceptions;

/**
 *
 * @author Michal HLavac <hlavki@hlavki.eu>
 */
public class XDXFDictionaryImporter extends DictionaryImporter {

    @Override
    public String getFormat() {
        return "xdxf";
    }

    @Override
    public String getDisplayName() {
        return "XDXF file importer";
    }

    @Override
    public void process(FileObject file) {
        XDXFParser parser = new DefaultXDXFParser();
        parser.addXDXFEventListener(new DictionaryListener(getCallback()));
        try {
            parser.parse(FileUtil.toFile(file));
        } catch (ParseException ex) {
            Exceptions.printStackTrace(ex);
        }
    }

    @Override
    protected String processDictionary(FileObject file) {
        XDXFParser parser = new DefaultXDXFParser();
        String name = null;
        try {
            XDXFDictionary dict = parser.parseDictionary(FileUtil.toFile(file));
            if (dict != null) {
                name = dict.getFullName();
            }
        } catch (ParseException ex) {
            Exceptions.printStackTrace(ex);
        }
        return name;
    }

    private static class DictionaryListener implements XDXFEventListener {

        private ImporterCallback callback;
        private Language srcLanguage;
        private Language targetLanguage;
        private boolean created;

        public DictionaryListener(ImporterCallback callback) {
            this.callback = callback;
            created = false;
        }

        public void onDictionary(XDXFDictionaryEvent evt) {
            // do nothing
        }

        public void onDictionaryChange(XDXFDictionaryEvent evt) {
            // do nothing
            XDXFDictionary dictionary = evt.getSource();
            if (!created && dictionary.getFullName() != null) {
                XDXFDictionary dict = evt.getSource();
                callback.onDictionaryCreate(dict.getFullName());
                this.srcLanguage = callback.onSourceLanguage(new Language(dict.getSrcLanguage()));
                if (dict.getSrcLanguage().equals(dict.getTargetLanguage())) {
                    this.targetLanguage = this.srcLanguage;
                } else {
                    this.targetLanguage = callback.onTargetLanguage(new Language(dict.getTargetLanguage()));
                }
                created = true;
            }
        }

        public void onArticle(XDXFArticleEvent evt) {
            XDXFArticle ar = evt.getSource();
            for (XDXFArticle.XDXFArticleKey key : ar.getKeys()) {
                Word srcWord = new Word(key.toString(), srcLanguage);
                Word targetWord = new Word(ar.getTranslation(), targetLanguage);
                callback.onRelationship(new WordRelationship(srcWord, targetWord, Relationship.TRANSLATION));
            }
        }
    }
}
